package edu.sctu;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;

public class UserEditServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");

        String id = req.getParameter("id");
        String userName = req.getParameter("userName");
        String password = req.getParameter("password");

        PrintWriter out = resp.getWriter();
        String sql = "UPDATE t_users " +
                "SET  userName = '" + userName + "',password='" + password + "'" +
                "WHERE id=" + id + "";

        out.println(userName + "编辑成功！");
        out.println("密码" + password);

        DBConnection dbConnection = new DBConnection();
        try {
            Connection connection = dbConnection.getConnection();
            Statement statement = connection.createStatement();
            statement.execute(sql);

            statement.close();
            dbConnection.closeConnection(connection);

        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        req.getRequestDispatcher("/userEdit.jsp").forward(req, resp);
    }
}

